package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.TypesGlassDao;
import com.fduss.artStore.entity.TypesGlass;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alex on 28/06/2017.
 */
public class TypesGlassDaoImpl implements TypesGlassDao {
    private JdbcUtil util;

    public TypesGlassDaoImpl() {
        util = new JdbcUtil();
    }

    @Override
    public TypesGlass add(TypesGlass typesGlass) {
        Connection conn = util.getConnection();
        String sql = "INSERT INTO `art`.`typesglass` " +
                "(`GlassID`, `Title`, `Description`, `Price`) VALUES (?, ?, ?, ?)";
        PreparedStatement pst = null;
        boolean flag = false;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesGlass.getGlassId());
            pst.setString(2, typesGlass.getTitle());
            pst.setString(3, typesGlass.getDescription());
            pst.setBigDecimal(4, typesGlass.getPrice());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
        return flag ? typesGlass : null;
    }

    @Override
    public void delete(int typesGlassId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`typesglass`  WHERE `GlassID` = ?";
        PreparedStatement pst = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesGlassId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public TypesGlass getOne(int typesGlassId) {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`typesglass` WHERE `GlassID` = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        TypesGlass typesGlass  = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesGlassId);
            rs = pst.executeQuery();

            if (rs.next()){
                typesGlass = buildTypesGlass(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return typesGlass;
    }

    @Override
    public List<TypesGlass> getAll() {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`typesglass`";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<TypesGlass> glasses = new ArrayList<>();

        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();

            while (rs.next()){
                glasses.add(buildTypesGlass(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }

        return glasses;
    }

    private TypesGlass buildTypesGlass(ResultSet rs) throws SQLException {
        TypesGlass typesGlass = new TypesGlass();
        typesGlass.setDescription(rs.getString("Description"));
        typesGlass.setGlassId(rs.getInt("GlassID"));
        typesGlass.setPrice(rs.getBigDecimal("Price"));
        typesGlass.setTitle(rs.getString("Title"));
        return typesGlass;
    }
}
